package src.ee.ioc.smaug.fsm;

/**
 * 
 * Mealy machine as one of FSM
 * @author Margarita Spichakova
 *
 */
public class MealyMachine extends FiniteStateMachine{
    /* (non-Javadoc)
     * @see src.ee.ioc.smaug.fsm.FiniteStateMachine#makeTransition(char)
     */
    public void makeTransition(char inputChar) {
        Transition tr = getTransition(transitionTable[currentState.getStateID()][this
                .getInputAlphabet().findPosition(inputChar)]);
        if (tr == null) {
            this.setRunning(false);
            System.err.println("Machine stopped, transition not found");
            return;
        } else {
            currentState = tr.getToState();
            setOutString(getOutString() + tr.getOutputSymbol());
        }
    }
   
    /* (non-Javadoc)
     * @see src.ee.ioc.smaug.fsm.FiniteStateMachine#runMachine()
     */
    public void runMachine() {
        this.setOutString("");
        currentState =  states.get(0);
        for (int i = 0; i < inString.length(); i++) {
            if (!this.isRunning()) {
                System.err.println("End of work");
                return;
            } else {
                makeTransition(inString.charAt(i));
            }
        }
    }
      
}
